package com.wtwd.campus.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wtwd.campus.entity.LocationSubmeter;
import com.wtwd.campus.entity.LocationSubmeterDto;
import com.wtwd.campus.model.vo.PageDataVO;
import com.wtwd.campus.model.vo.ResultVO;
import com.wtwd.campus.service.LocationSubmeterService;
import com.wtwd.campus.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author mjy
 * @date 2022/12/02
 * @description
 */
@Api(tags = "分表查询定位轨迹")
@RestController
@RequestMapping("/location-submeter")
public class LocationSubmeterController {

    @Resource
    private LocationSubmeterService locationSubmeterService;


    @ApiOperation("根据imei获取设备定位数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "imei", example = "imei", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "startTime", example = "开始时间", required = true, dataType = "long.class"),
            @ApiImplicitParam(name = "endTime", example = "结束时间", required = true, dataType = "long.class")
    })
    @GetMapping("/queryDeviceLocationByImei")
    public ResultVO<List<LocationSubmeter>> queryDeviceLocationByImei(@RequestParam("imei") String imei,
                                                                      @RequestParam("startTime") long startTime,
                                                                      @RequestParam("endTime") long endTime) {
        return ResultVO.ok(locationSubmeterService.queryLocationSubmeterByImeiAndTime(imei, startTime, endTime));
    }

    @ApiOperation("根据imei分页获取设备定位数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "imei", example = "imei", required = true, dataTypeClass = String.class)
            , @ApiImplicitParam(name = "pageNo", value = "页数", required = true, dataTypeClass = Integer.class)
            , @ApiImplicitParam(name = "pageSize", value = "页大小", required = true, dataTypeClass = Integer.class)
    })
    @GetMapping("/queryDeviceLocationPageByImei")
    public ResultVO<PageDataVO<LocationSubmeter>> queryDeviceLocationPageByImei(@RequestParam("imei") String imei,
                                                                                @RequestParam(value = "pageNo", defaultValue = "0") Integer pageNo,
                                                                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<LocationSubmeter> page = new Page<>(pageNo, pageSize);
        IPage<LocationSubmeter> locations = locationSubmeterService.queryDeviceLocationPageByImei(imei, page);
        if (locations!=null){
            List<LocationSubmeter> records = locations.getRecords();
            if (records != null && records.size() > 0) {
                records.stream().forEach(e -> {
                    e.setLocationTimeStr(timestampToFormattedString(e.getLocationTime(), DateUtils.dateFormat4));
                });
            }
        }
        return ResultVO.ok(locations);
    }


    /**
     * 存储定位信息
     *
     * @param locationSubmeterDto
     * @return
     */
    @ApiOperation("存储定位数据")
    @PostMapping("/save")
    public ResultVO save(@RequestBody LocationSubmeterDto locationSubmeterDto) {
        LocationSubmeter locationSubmeter = new LocationSubmeter();
        BeanUtils.copyProperties(locationSubmeterDto, locationSubmeter);
        locationSubmeterService.save(locationSubmeter, locationSubmeterDto.getImei());
        return ResultVO.ok();
    }

    public static String timestampToFormattedString(long timestamp, String format) {
        // 将时间戳转换为 LocalDateTime 对象
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
        // 加上半小时
        dateTime = dateTime.plusMinutes(30);
        // 使用指定的格式化程序格式化日期时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return dateTime.format(formatter);
    }

}
